package com.aiatomic.couting.widgets;

import java.util.List;

import com.aiatomic.couting.R;
import com.aiatomic.couting.models.TemplateEntity;
import com.aiatomic.couting.models.TemplateGroupEntity;
import com.aiatomic.couting.templates.ITemplateChangeEventListener;
import com.aiatomic.couting.templates.TemplateChangeEventDispatcher;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class TemplateAdapter extends ArrayAdapter<TemplateGroupEntity> {

	public TemplateAdapter(Context context, int resource, List<TemplateGroupEntity> objects) {
		super(context, resource, objects);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		MyViewHolder holder = null;
		TemplateGroupEntity dataItem = this.getItem(position);
		if (null == convertView) {
			holder = new MyViewHolder();
			convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item_count_template, null);

			holder.recycleView = (RecyclerView) convertView.findViewById(R.id.recycler_view_list);
			// 设置布局管理器
			LinearLayoutManager linearLayOut = new LinearLayoutManager(this.getContext());
			linearLayOut.setOrientation(LinearLayoutManager.HORIZONTAL);
			holder.recycleView.setLayoutManager(linearLayOut);
			// 设置adapter
			// 设置Item增加、移除动画
			holder.recycleView.setItemAnimator(new DefaultItemAnimator());
			convertView.setTag(holder);
		} else {
			holder = (MyViewHolder) convertView.getTag();
		}
		holder.txtTitle = (TextView) convertView.findViewById(R.id.itemTitle);
		holder.txtTitle.setText(dataItem.getGroup_name());
		holder.recycleView.setAdapter(new TemplateSingleAdapter(dataItem, getContext()));
		return convertView;
	}

	class MyViewHolder {
		TextView txtTitle;
		RecyclerView recycleView;
	}
}

class TemplateSingleAdapter extends RecyclerView.Adapter<TemplateSingleAdapter.MyViewHolder>
		implements ITemplateChangeEventListener {
	private TemplateGroupEntity templateGrp;
	private Context context;

	public TemplateSingleAdapter(TemplateGroupEntity templateGrp, Context context) {
		super();
		TemplateChangeEventDispatcher.getInstance().registListener(this);
		this.templateGrp = templateGrp;
		this.context = context;
	}

	@Override
	public int getItemCount() {
		return templateGrp.getTemplates().size();
	}

	@Override
	public void onBindViewHolder(MyViewHolder holder, int position) {
		holder.initData(templateGrp.getTemplates().get(position));
	}

	@Override
	public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		MyViewHolder holder = new MyViewHolder(
				LayoutInflater.from(context).inflate(R.layout.list_single_card_count_template, parent, false));
		return holder;
	}

	class MyViewHolder extends RecyclerView.ViewHolder {
		private TemplateEntity data;

		public MyViewHolder(View view) {
			super(view);
		}

		public void initData(TemplateEntity entity) {
			data = entity;
			((TextView) this.itemView.findViewById(R.id.tvTitle)).setText(data.getName());
			((TextView) this.itemView.findViewById(R.id.tvVersion))
					.setText(String.format(itemView.getContext().getString(R.string.template_version),
							data.getGroup_id() * 1000 + data.getSb_template_id(), data.getVersion()));
			ImageView snap = (ImageView) this.itemView.findViewById(R.id.itemImage);
			if (data != null && !data.getPic().trim().isEmpty()) {
				DownloadImageTask task = new DownloadImageTask(snap, R.string.dir_cache_template_store);
				task.execute(data.getPic());
			}
			Button btn = (Button) this.itemView.findViewById(R.id.addremove_btn);
			if (data.isDownload()) {
				btn.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.ic_template_delete));
			} else {
				btn.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.ic_template_add));
			}
			btn.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Button btn = (Button) v;
					if (data.isDownload()) {
						TemplateChangeEventDispatcher.getInstance().dispatchEvent(data, TemplateChangeEventDispatcher.EVENT_CODE_REMOVE);
					} else {
						TemplateChangeEventDispatcher.getInstance().dispatchEvent(data, TemplateChangeEventDispatcher.EVENT_CODE_ADD);
					}
				/*	if (data.isDownload()) {
						TemplateChangeEventDispatcher.getInstance().dispatchEvent(data, TemplateChangeEventDispatcher.EVENT_CODE_ADD);
						//btn.setBackground(context.getDrawable(R.drawable.ic_template_delete));
					} else {
						TemplateChangeEventDispatcher.getInstance().dispatchEvent(data, TemplateChangeEventDispatcher.EVENT_CODE_REMOVE);
						//btn.setBackground(context.getDrawable(R.drawable.ic_template_add));
					}*/

				}
			});
		}
	}

	@Override
	public void onTemplateChangeEvent(TemplateEntity entity, int eventCode) {
		switch (eventCode) {
		case TemplateChangeEventDispatcher.EVENT_CODE_ADD:
			for (TemplateEntity tpl : templateGrp.getTemplates()) {
				if (tpl.equals(entity)) {
					tpl.setDownload(true);
					TemplateSingleAdapter.this.notifyDataSetChanged();
					break;
				}
			}
			break;
		case TemplateChangeEventDispatcher.EVENT_CODE_REMOVE:
			for (TemplateEntity tpl : templateGrp.getTemplates()) {
				if (tpl.equals(entity)) {
					tpl.setDownload(false);
					TemplateSingleAdapter.this.notifyDataSetChanged();
					break;
				}
			}
			break;
		}
	}

}
